import FN from '../../utils/public'
const {configApp} = require('../../utils/config.js');
const qiniuUploader = require("../../utils/qiniuUploader");
import loginAPI from '../../api/loginAPI'
const app = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   * 
   */
  data: {
    avatarUrl: defaultAvatarUrl,
    nickName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    this.uploadUserHeaderImg(avatarUrl)
  },

  uploadUserHeaderImg(avatarUrl) {
    var that = this, filePath = avatarUrl, fileName = "avatarUrl" + new Date().getTime() + ".png";
    this.initQiniu();
    // 向七牛云上传
    qiniuUploader.upload(filePath, (res) => {
      res.fileName = fileName;
      if (res.hasOwnProperty("error")) {
        wx.showToast({
          title: '上传失败，请重新上传',
          icon: 'none',
          duration: 2000
        })
        that.initQiniu();
      } else {
        this.setData({
          avatarUrl: res.fileURL
        })
      }
    }, (error) => {
      console.error('error: ' + JSON.stringify(error));
    }, {key:fileName}, (progress) => {
    }, cancelTask => that.setData({ cancelTask })
    );
  },

  initQiniu: function () {
    var options = {
      region: 'ECN',
      uptokenURL: configApp.api_base_url+'qiniu/getUploadToken',
      domain: configApp.qiniuBucketURLPrefix,
      shouldUseQiniuFileName: true
    }
    // 将七牛云相关配置初始化进本sdk
    qiniuUploader.init(options);
  },

  // 监听昵称改变
  onChangeNickname(e) {
    this.setData({
      nickName: e.detail
    })    
  },

  // 提交
  formSubmit(e) {
    const userInfo = {
      avatarUrl: this.data.avatarUrl,
      nickName: this.data.nickName
    }
    this.saveWXinfo(userInfo);
  },

  uploadLocalServer(avatarUrl) {
    //  将头像上传到服务器
    wx.uploadFile({
      url: config.configApp.api_base_url + 'uploadImg',
      filePath: avatarUrl,
      name: 'file',
      success(res) {
        const data = res.data
      },
      fail: function (res) {
        console.log(res);//发送失败回调，可以在这里了解失败原因
      }
    })
  },

  // 保存用户信息
  saveWXinfo: function (userInfo) {
    var that = this;
    wx.login({
      success(result) {
        userInfo.code = result.code
        if (result.code) {
          loginAPI.codetransformationOpenId(userInfo).then(res => {
            if (res.code == '200') {
              // 提交成功
              FN.Toast(res.data)
            }
            app.setPageTabBarBadge();
            setTimeout(() => {
              wx.switchTab({
                url: '../my/my'
              })
            }, 2000);
          })
        }
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (app.globalData.userInfo.signInfo) {
      this.setData({
        avatarUrl: app.globalData.userInfo.signInfo.avatarUrl,
        nickName: app.globalData.userInfo.signInfo.nickName
      })
    }
  },
})